package com.example.medmobile.profissional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.example.medmobile.R;
import com.google.appengine.medmobileserver.business.Paciente;



public class ListaAgendamentoProfissionalAdapter extends BaseAdapter {

	private Context context;
	private List<com.google.appengine.medmobileserver.business.Agendamento> lista;

	public ListaAgendamentoProfissionalAdapter(
			Context context,
			List<com.google.appengine.medmobileserver.business.Agendamento> lista) {
		this.context = context;
		this.lista = lista;

	}

	public int getCount() {
		return lista.size();
	}

	public Object getItem(int i) {
		return lista.get(i);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int posicao, View converView, ViewGroup parent) {
		com.google.appengine.medmobileserver.business.Agendamento agendamento = lista
				.get(posicao);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(
				R.layout.activity_lista_agendamento_profissional_adapter, null);
		TextView textNome = (TextView) v
				.findViewById(R.id.AdapterAgendamentoNomeMedico);
		TextView textEndereco = (TextView) v
				.findViewById(R.id.AdapterAgendamentoEndereco);

		TextView textStatus = (TextView) v
				.findViewById(R.id.AdapterAgendamentoStatusConsulta);
		TextView textHorario = (TextView) v
				.findViewById(R.id.AdapterAgendamentoHorarioConsulta);
		textNome.setText(agendamento.getPaciente().getNome());
		textEndereco.setText(new SimpleDateFormat("dd/MM/yyyy")
				.format(agendamento.getDataHora()));

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");	
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		
		textHorario.setText(sdf.format(agendamento.getDataHora().getTime()));
		textStatus.setText(agendamento.getStatus());

		if (agendamento.getStatus().equals("Confirmado"))
			textStatus.setTextColor(context.getResources().getColor(
					R.color.verde));
		else if (agendamento.getStatus().equals("Pendente"))
			textStatus.setTextColor(context.getResources().getColor(
					R.color.laranja));
		else if (agendamento.getStatus().equals("Cancelado"))
			textStatus.setTextColor(context.getResources().getColor(
					R.color.vermelho));

		v.setTag(agendamento.getIdAgendamento());
		CheckBox cbSelect = (CheckBox) v
				.findViewById(R.id.chAgendamentoSelecionado);
		cbSelect.setTag(posicao);
		cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				int i = (Integer) buttonView.getTag();
				View vi = buttonView.getRootView();
				View v2 = (View) buttonView.getParent();
				View v3 = (View) v2.getParent();
				v3.setSelected(isChecked);

				TextView textHorario = (TextView) v3
						.findViewById(R.id.AdapterAgendamentoHorarioConsulta);

			}
		});
		v.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Paciente p =  lista.get(posicao).getPaciente();
				Intent intent = new Intent();
				Bundle param = new Bundle();
				String sJson = com.cedarsoftware.util.io.JsonWriter.toJson(p);

				param.putString("Paciente", sJson);
				intent.putExtras(param);
				intent.setClass(context, PerfilPaciente.class);
				context.startActivity(intent);

			}
		});

		return v;
	}

}
